package org.spring.springboot.app.sys.role.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.sys.role.domain.vo.*;
import org.spring.springboot.app.sys.role.service.ISysRoleService;
import org.spring.springboot.app.sys.systemoption.service.ISystemOptionService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "角色操作接口",tags = ApiIndex.SysRoleController)
@RequestMapping(value = "/api/sysRole")
@RestController
@AccessTokenVerify
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @ApiOperation(value = "分页查询角色")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<PageInfo<List<SysRoleResVO>>> findPage(
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ) {
        R result = new R();
        List<SysRoleResVO> list = sysRoleService.findPage(pageNum, pageSize);
        result.setData(new PageInfo(list));
        return result;
    }

    @ApiOperation(value = "查询所有角色")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<SysRoleResVO>> findAll() {
        R result = new R();
        List<SysRoleResVO> list = sysRoleService.findAll();
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "查询角色菜单")
    @RequestMapping(value = "/roleMenu", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<PageInfo<List<SysRoleResVO>>> findRoleMenu(
            @ApiParam(value = "角色ID", required = true) @RequestParam String id
    ) {
        R result = new R();
        List<RoleMenuResVO> list = sysRoleService.findRoleMenu(id);
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "查询角色可管理角色")
    @RequestMapping(value = "/roleManage", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<List<SysRoleResVO>> findRoleManage(
            @ApiParam(value = "当前角色ID", required = true) @RequestParam String id
    ) {
        R result = new R();
        List<SysRoleResVO> list = sysRoleService.findManageRole(id);
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "保存角色可管理角色")
    @RequestMapping(value = "/roleManage", method = RequestMethod.POST)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R saveRoleManage(
            @ApiParam(value = "角色对象", required = true) @RequestBody RoleManageReqVO vo
    ) {
        R result = sysRoleService.saveRoleManage(vo);
        return result;
    }

    @ApiOperation(value = "根据Id获取单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R<SysRoleResVO> find(
            @ApiParam(value = "角色id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        SysRoleResVO data = sysRoleService.find(id);
        result.setData(data);
        return result;
    }


    @ApiOperation(value = "保存角色信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R save(
            @ApiParam(value = "角色对象", required = true) @RequestBody SysRoleReqVO sysRole
    ) {
        R result = sysRoleService.save(sysRole);
        return result;
    }


    @ApiOperation(value = "修改角色信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R update(
            @ApiParam(value = "角色对象", required = true) @RequestBody SysRoleReqVO sysRole
    ) {
        return sysRoleService.update(sysRole);
    }

    @ApiOperation(value = "删除角色信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    public R delete(
            @ApiParam(value = "角色id", required = true) @PathVariable("id") String id
    ) {
        return sysRoleService.delete(id);
    }
}
